<?php
/**
 * A hack fix for templating
 * May change. VOLITILE
 * 
 * XXX htmlspecialchars(), urlencode(), bbcode()
 **/
session_start();

function tplUserLoginRequestBar() {
  if( !isset($_SESSION['uid']) ) {
    tplUserLogin(); tplUserRegisterButton();
  } else {
    tplHelloUserButton(); tplUserLogoutButton();
  }
}
function tplUserRegister() {
  echo <<<EOO
        <form action="./user.php" method="post" >
          <input type="hidden" name="type" value="REGISTER" />
          name: <input type="text" name="user" value="user name" /> <br />
          pass: <input type="password" name="pass" value="password" /> <br />
          email: <input type="text" name="email" value="email@where.com" /> <br />
          <input type="submit" value="register">
        </form>
EOO;
}
function tplUserLogin() {
  echo <<<EOO
        <form action="./user.php" method="post" >
          <input type="hidden" name="type" value="LOGIN" />
          name: <input type="text" name="user" value="user name" /> 
          pass: <input type="password" name="pass" value="password" />
          <input type="submit" value="login">
        </form>
EOO;
}
function tplUserRegisterButton() {
  echo '<a href="./register.php">register?</a>';
}
function tplHelloUserButton() {
  echo "Welcome {$_SESSION['user']['name']}! Have fun!";
}
function tplUserLogoutButton() {
  echo '<a href="./user.php?type=LOGOUT">bye-bye?</a>';
}

function tplHead() { 
?>
<html>
 <head>
 </head>
 <body>
<?php
}

function tplFoot() {
?>
 </body>
</html>
<?php
}

function tplPost( $post ) {
  $date = $post['date'];
  echo <<<EOE
<h1>{$post['name']} on $date by {$post['uname']}</h1><br />
<p>{$post['datum']}</p>
EOE;
}
function tplPostEditButton( $post ) {
  echo <<<EOE
<a href="post.php?tnum=${post['tid']}&pnum=${post['rank']}" >EDIT</a>
EOE;
}
function tplPostReplyButton( $post ) {
  echo <<<EOE
<a href="post.php?tnum=${post['tid']}" >Reply</a>
EOE;
}

function tplThread( $thread ) {
  echo "<p><a href=\"spit.php?type=POST&tnum=${thread['id']}&start=0&inc=5\">${thread['name']} - ${thread['datum']}</a></p>";
}
function tplForum( $forum ) {
  echo "<p><a href=\"spit.php?type=THREAD&fnum=${forum['id']}&start=0&inc=5\">${forum['name']} - ${forum['datum']}</a></p>";
}

function tplEditPost( $post ) {
echo <<<EOE
<form action="./gulp.php" method="post" >
  <input type="hidden" name="type" value="POST" />
  <input type="hidden" name="pnum" value="${post['rank']}" />
  <input type="hidden" name="tnum" value="${post['tid']}" />
  
  title:
  <input type="text" name="title" value="${post['name']}" /><br />
  text:
  <textarea name="datum" rows="10" cols="30">${post['datum']}</textarea><br />
  <input type="submit" name="action[edit]" value="DOOM">
</form>

EOE;
}
function tplNewPost( $tnum, $fnum ) {
echo <<<EOE
<form action="./gulp.php" method="post" >
  <input type="hidden" name="type" value="POST" />
  <input type="hidden" name="tnum" value="$tnum" />
  <input type="hidden" name="fnum" value="$fnum" />
  
  title:
  <input type="text" name="title" value="" /><br />
  text:
  <textarea name="datum" rows="10" cols="30">
    Insert Text Har. Mmkays
  </textarea><br />
  <input type="submit" name="action[new]" value="DOOM">
</form>

EOE;
}
function tplNewThread( $fnum ) {
echo <<<EOE
<form action="./gulp.php" method="post" >
  <input type="hidden" name="type" value="POST" />
  <input type="hidden" name="fnum" value="$fnum" />
  
  title:
  <input type="text" name="title" value="" /><br />
  text:
  <textarea name="datum" rows="10" cols="30">
    Insert Text Har. Mmkays
  </textarea><br />
  <input type="submit" name="action[new]" value="DOOM">
</form>

EOE;
}

function tplNewThreadButton( $fnum ) {
  echo <<<EOE
<a href="post.php?fnum=${fnum}" >New Thread</a>
EOE;
}

function tplEditForum( $forum ) {
  echo <<<EOO
        <form action="./gulp.php" method="post" >
          <input type="hidden" name="type" value="FORUM" />
          <input type="hidden" name="fnum" value="${forum['id']}" />
          <input type="text" name="title" value="${forum['name']}" />
          <input type="text" name="datum" value="${forum['datum']}" />
          <input type="submit" name="action[edit]" value="save">
          <input type="submit" name="action[delete]" value="delete">
        </form>
EOO;
}
function tplNewForum( ) {
  echo <<<EOO
        <form action="./gulp.php" method="post" >
          <input type="hidden" name="type" value="FORUM" />
          <input type="hidden" name="fnum" value="-1" />
          <input type="text" name="title" value="" />
          <input type="text" name="datum" value="" />
          <input type="submit" name="action[new]" value="new">
        </form>
EOO;
}
function tplForumEditButton() {
  echo <<<EOE
<a href="admin.php" >Edit Forums</a>
EOE;
}

function tplPostPagerBar($tnum, $count, $curr, $inc) {
  $maxpage = ceil($count/$inc);
  $currpage = 1+ (($curr > 0) ? $curr/$inc : 0);
  
  if( $curr > 1 ) {
    $page = $curr - $inc;
    $first = "<a href=\"spit.php?type=POST&tnum=$tnum&start=0&inc=$inc\">[FIRST]</a>";
    $prev = "<a href=\"spit.php?type=POST&tnum=$tnum&start=$page&inc=$inc\">[PREV]</a>";
  } else {
    $prev = ' [Prev] ';
    $first = ' [First Page] ';
  }

  if( $currpage < $maxpage ) {
    $page = $curr + $inc;
    $maxnum = $count - $inc;
    $next = "<a href=\"spit.php?type=POST&tnum=$tnum&start=$page&inc=$inc\">[NEXT]</a>";
    $last = "<a href=\"spit.php?type=POST&tnum=$tnum&start=$maxnum&inc=$inc\">[LAST]</a>";
  } else {
    $next = ' [NEXT] ';
    $last = ' [LAST] ';
  }
  
  if( $maxpage > 1 ) {
    echo $first . $prev . $nav . $next . $last;
  }
}
function tplThreadPagerBar($fnum, $count, $curr, $inc) {
  $maxpage = ceil($count/$inc);
  $currpage = 1+ (($curr > 0) ? $curr/$inc : 0);


  if( $curr > 1 ) {
    $page = $curr - $inc;
    $first = "<a href=\"spit.php?type=THREAD&fnum=$fnum&start=0&inc=$inc\">[FIRST]</a>";
    $prev = "<a href=\"spit.php?type=THREAD&fnum=$fnum&start=$page&inc=$inc\">[PREV]</a>";
  } else {
    $prev = ' [Prev] ';
    $first = ' [First Page] ';
  }

  if( $currpage < $maxpage ) {
    $page = $curr + $inc;
    $maxnum = $count - $inc;
    $next = "<a href=\"spit.php?type=THREAD&fnum=$fnum&start=$page&inc=$inc\">[NEXT]</a>";
    $last = "<a href=\"spit.php?type=THREAD&fnum=$fnum&start=$maxnum&inc=$inc\">[LAST]</a>";
  } else {
    $next = ' [NEXT] ';
    $last = ' [LAST] ';
  }
  
  if( $maxpage > 1 ) {
    echo $first . $prev . $nav . $next . $last;
  }
}

function tplDeleteThreadButton($fid, $tid) {
  echo "<a href=\"gulp.php?type=THREAD&action[delete]=true&fnum=$fid&tnum=$tid\">DELeTE</a>";
}
function tplDeletePostButton($tid, $pid) {
  echo "<a href=\"gulp.php?type=POST&action[delete]=true&tnum=$tid&pnum=$pid\">DELETE</a>";
}

?>
